//
//  NSObject+UITableView.m
//  OCPopular
//
//  Created by Jarvis on 2024/5/31.
//

#import "NSObject+UITableView.h"
#import <objc/runtime.h>

@implementation NSObject (UITableView)

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [UIView new];
    //header.backgroundColor = UIColor.redColor;
    return header;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footer = [UIView new];
    //footer.backgroundColor = UIColor.greenColor;
    return footer;
}

@end



@interface UITableView (TopPadding)

@end
@implementation UITableView (TopPadding)
+ (void)load {
    Method m1 = class_getInstanceMethod(self, @selector(initWithFrame:style:));
    Method m2 = class_getInstanceMethod(self, @selector(hook_initWithFrame:style:));
    method_exchangeImplementations(m1, m2);
}

- (instancetype)hook_initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    UITableView *tv = [self hook_initWithFrame:frame style:style];
    if (style == UITableViewStylePlain) {
        if (@available(iOS 15.0, *)) {
            self.sectionHeaderTopPadding = 0;
            self.insetsContentViewsToSafeArea = YES;
            // 页面单独设置
            //tableView.sectionHeaderTopPadding = 0;
            // 全局设置
            //UITableView.appearance.sectionHeaderTopPadding = 0.0;
        }
    }
    return tv;
}
@end
